package com.dongdong.aop;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

//放入容器
@Component
//加入aop注解
@Aspect

@Slf4j
public class AopLog {

    //定义切面
    @Pointcut("execution(public * com.dongdong.service.*.*(..))||@annotation(com.dongdong.aop.LogAN)")
    public void log(){}

    //前置通知
    @Before("log()")
    public void beforeLog(JoinPoint joinPoint){
        //获取方法
        Signature signature = joinPoint.getSignature();
        //获取参数
        Object[] args = joinPoint.getArgs();
        log.info("当前方法为:{},参数为:{}",signature,args);
    }

    //返回通知
    @AfterReturning(value = "log()",returning = "result")
    public void AfterReturningLog(Object result){
        log.info("返回值为:{}", JSONObject.toJSONString(result));
    }
}
